var e = require("../../requests/request.js"), a = getApp();

Page({
    data: {
        awardList: [],
        colorAwardDefault: "#f8f8f8",
        colorAwardSelect: "#FEED57",
        indexSelect: -1,
        isRunning: !1,
        imageAward: [],
        times: 60,
        label: "",
        selectAwardName: "",
        selectAwardPic: "",
        existAwardName: "",
        existTime: "",
        disableGame: !1,
        alertNumber: 0,
        description: "",
        userAwardList: [],
        closeShowSecond: 3
    },
    selectuserawardinfo: function() {
        var a = this, t = a.data.label, i = wx.getStorageSync("opid");
        null != i && "" != i || wx.login({
            success: function(a) {
                a.code && e.getOpenId(a.code, function(e) {
                    i = e.openid;
                });
            }
        }), e.selectuserawardinfo(i, t, function(e) {
            if (2 == e.Status) {
                var t = e.Data[0].AwardName, i = e.Data[0].OperationTime;
                a.setData({
                    existAwardName: t,
                    existTime: i.substring(5, 16),
                    disableGame: !0
                });
            }
        });
    },
    onLoad: function(e) {
        var a = this, t = e.label;
        this.setData({
            label: t
        }), a.selectActivityAwardList(), a.selectuserawardinfo(), a.selectuserawardlist();
    },
    createuserawardinfo: function(a) {
        var t = this, i = t.data.label, n = wx.getStorageSync("opid"), r = wx.getStorageSync("UserId"), s = wx.getStorageSync("UserName");
        null != n && "" != n || wx.login({
            success: function(a) {
                a.code && e.getOpenId(a.code, function(e) {
                    n = e.openid;
                });
            }
        }), e.createuserawardinfo(n, i, r, s, function(e) {
            if (2 == e.Status) {
                for (var i = e.Data[0].ActivityAwardId, n = 0; n < t.data.imageAward.length; n++) if (t.data.imageAward[n].awardid == i) return void a({
                    indexSelect: n,
                    selectAwardName: t.data.imageAward[n].name,
                    selectAwardPic: t.data.imageAward[n].url
                });
            } else wx.showToast({
                title: "活动已经结束！",
                icon: "none"
            }), a({
                indexSelect: -1,
                selectAwardName: "",
                selectAwardPic: ""
            });
        });
    },
    existUserAward: function() {
        var e = this;
        e.setData({
            alertNumber: 1,
            closeShowSecond: 3
        });
        var a = e.data.closeShowSecond, t = setInterval(function() {
            a -= 1, e.setData({
                closeShowSecond: a
            }), 0 == a && (clearInterval(t), 1 == e.data.alertNumber && wx.switchTab({
                url: "../index/index"
            }));
        }, 1e3);
    },
    startGame: function() {
        var e = this;
        if (!this.data.isRunning) {
            this.setData({
                isRunning: !0
            });
            var a = -1, t = "", i = "";
            this.createuserawardinfo(function(e) {
                a = e.indexSelect, t = e.selectAwardName, i = e.selectAwardPic;
            });
            var n = e.data.indexSelect, r = 0, s = setInterval(function() {
                if (-1 != a) {
                    if (n %= 8, e.setData({
                        indexSelect: n
                    }), r += 90, n++, r <= 1e3 && r > 800) {
                        clearInterval(s);
                        var c = setInterval(function() {
                            n %= 8, e.setData({
                                indexSelect: n
                            }), (r += 90) > 1e3 && n % 8 == a ? (clearInterval(c), e.setData({
                                indexSelect: a,
                                selectAwardName: t,
                                selectAwardPic: i
                            }), setTimeout(function() {
                                var a = 2;
                                "谢谢参与" == t && (a = 3), e.setData({
                                    isRunning: !1,
                                    alertNumber: a
                                }), e.selectuserawardinfo();
                            }, 1500)) : n++;
                        }, 300);
                    }
                } else e.setData({
                    isRunning: !1
                });
            }, 100);
        }
    },
    btnClick: function() {
        this.setData({
            alertNumber: 0,
            closeShowSecond: 3
        });
    },
    selectActivityAwardList: function() {
        var a = this, t = [];
        e.selectactivityawardlist(a.data.label, function(e) {
            if (2 == e.Status) {
                a.setData({
                    descripiton: e.Description
                });
                for (c = 0; c < e.Data.length; c++) {
                    var i = {
                        url: e.Data[c].AwardPicUrl,
                        name: e.Data[c].AwardName,
                        awardid: e.Data[c].ActivityAwardId
                    };
                    t.push(i);
                }
                for (var n = [], r = 10, s = 10, c = 0; c < 8; c++) {
                    0 == c ? (r = 10, s = 10) : c < 3 ? (r = r, s = s + 190 + 10) : c < 5 ? (s = s, 
                    r = r + 190 + 10) : c < 7 ? (s = s - 190 - 10, r = r) : c < 8 && (s = s, r = r - 190 - 10);
                    var d = t[c];
                    n.push({
                        topAward: r,
                        leftAward: s,
                        imageAward: d
                    });
                }
                a.setData({
                    awardList: n,
                    imageAward: t
                });
            }
        });
    },
    selectuserawardlist: function() {
        var a = this;
        e.selectuserawardlist(a.data.label, function(e) {
            if (2 == e.Status) {
                for (var t = [], i = 0; i < e.Data.length; i++) {
                    var n = {
                        UserName: e.Data[i].UserName,
                        AwardName: e.Data[i].AwardName
                    };
                    t.push(n);
                }
                a.setData({
                    userAwardList: t
                });
            }
        });
    },
    returnBtn: function() {
        a.gotoIndex();
    },
    onReady: function() {},
    onShow: function() {
        this.selectuserawardinfo();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});